package com.putao.album.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;

import com.putao.album.R;

/**
 * Created by jidongdong on 15/5/6.
 */
public abstract class DialogBuilder implements IDialogBuilder {

    protected Context context;
    protected String message;
    protected String positiveButtonText;
    protected String negativeButtonText;
    protected DialogInterface.OnClickListener positiveButtonClickListener, negativeButtonClickListener;

    private Dialog mDialog;

    public DialogBuilder(Context context) {
        this.context = context;
    }


    public DialogBuilder setMessage(String message) {
        this.message = message;
        return this;
    }

    public String getMessage() {
        return this.message;
    }

    public String getPositiveButtonText() {
        return this.positiveButtonText;
    }

    public String getNegativeButtonText() {
        return this.negativeButtonText;
    }

    public DialogBuilder setMessage(int message) {
        this.message = (String) context.getText(message);
        return this;
    }

    public DialogBuilder setPositiveButton(int positiveButtonText, DialogInterface.OnClickListener listener) {
        this.positiveButtonText = (String) context.getText(positiveButtonText);
        this.positiveButtonClickListener = listener;
        return this;
    }


    public DialogBuilder setPositiveButton(String positiveButtonText, DialogInterface.OnClickListener listener) {
        this.positiveButtonText = positiveButtonText;
        this.positiveButtonClickListener = listener;
        return this;
    }

    public DialogBuilder setPositiveButton(String positiveButtonText) {
        this.positiveButtonText = positiveButtonText;
        this.positiveButtonClickListener = null;
        return this;
    }

    public DialogBuilder setPositiveButton(int positiveButtonText) {
        this.positiveButtonText = (String) context.getText(positiveButtonText);
        this.positiveButtonClickListener = null;
        return this;
    }


    public DialogBuilder setNegativeButton(int negativeButtonText, DialogInterface.OnClickListener listener) {
        this.negativeButtonText = (String) context.getText(negativeButtonText);
        this.negativeButtonClickListener = listener;
        return this;
    }

    public DialogBuilder setNegativeButton(int negativeButtonText) {
        this.negativeButtonText = (String) context.getText(negativeButtonText);
        this.negativeButtonClickListener = null;
        return this;
    }

    public DialogBuilder setNegativeButton(String negativeButtonText) {
        this.negativeButtonText = negativeButtonText;
        this.negativeButtonClickListener = null;
        return this;
    }

    public DialogBuilder setNegativeButton(String negativeButtonText, DialogInterface.OnClickListener listener) {
        this.negativeButtonText = negativeButtonText;
        this.negativeButtonClickListener = listener;
        return this;
    }


    protected abstract int getContentViewId();

    protected abstract void doInitSubView(View view);

    protected abstract void doSetupData();

    protected void setWindowAnim(Dialog dialog) {

    }


    public Dialog Create() {
        mDialog = new Dialog(context, R.style.pt_dialog_style);
        mDialog.setCanceledOnTouchOutside(false);
        int layout_id = getContentViewId();
        if (layout_id <= 0) {
            throw new IllegalArgumentException("content view is illegal");
        }
        View layout = LayoutInflater.from(context).inflate(layout_id, null);
        setWindowAnim(mDialog);
        mDialog.setContentView(layout);
        doInitSubView(layout);
        doSetupData();
        return mDialog;
    }

    public Dialog getDialog() {
        return mDialog;
    }
}
